<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="../assets/js/polygon.min.js"></script> 
<script type="text/javascript">
	var peta;
	var gambar_tanda;
	
	function set_icon(ikon){
		var icon_temp = ikon.split(',');
		var icon_sektoral = icon_temp[1];
		gambar_tanda = "../file/icon/"+icon_sektoral;
	}
	
	function peta_awal(){
	    var lokasibaru = new google.maps.LatLng(-6.912889,107.609787);
    	var petaoption = {
			zoom: 13,
			center: lokasibaru,
			mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
	    peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
	    
	    tanda = new google.maps.Marker({
	        position: lokasibaru,
	        map: peta, 
	        icon: gambar_tanda,
	        draggable : true
	    });
	    
		 var creator = new PolygonCreator(peta);
		 
		 //reset
		 $('#reset').click(function(){ 
		 		creator.destroy();
		 		creator=null;
		 		creator=new PolygonCreator(peta);
		 });		 
		 
		 
		 //show paths
		 $('#showData').click(function(){ 
		 		$('#dataPanel').empty();
		 		if(null==creator.showData()){
		 			$('#dataPanel').append('Please first create a polygon');
		 		}else{
		 			$('#dataPanel').append(creator.showData());
		 		}
		 });
		 
	    google.maps.event.addListener(tanda, 'dragend', function(event){
				document.getElementById('latitude').value = this.getPosition().lat();
				document.getElementById('longitude').value = this.getPosition().lng();
		});
		
		
		
	}
	
	

	
	function kasihtanda(lokasi){
	    $("#form_lokasi").slideDown();
	    $("#latitude").val(lokasi.lat());
	    $("#longitude").val(lokasi.lng());
	    tanda = new google.maps.Marker({
	        position: lokasi,
	        map: peta
	    });
	}
	
	function setpeta(lokasi){
	var koordinat = lokasi.split(',');
	var x = koordinat[0];
	var y = koordinat[1];
	var id = koordinat[2];
    var lokasibaru = new google.maps.LatLng(x, y);
    var petaoption = {
        zoom: 13,
        center: lokasibaru,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
    
     var creator = new PolygonCreator(peta);
		 
		 //reset
		 $('#reset').click(function(){ 
		 		creator.destroy();
		 		creator=null;
		 		creator=new PolygonCreator(peta);
		 });		 
		 
		 
		 //show paths
		 $('#showData').click(function(){ 
		 		$('#dataPanel').empty();
		 		if(null==creator.showData()){
		 			$('#dataPanel').append('Please first create a polygon');
		 		}else{
		 			$('#dataPanel').append(creator.showData());
		 		}
		 });
    tanda = new google.maps.Marker({
        position: lokasibaru,
        icon: gambar_tanda,
        map: peta, draggable : true
    });

    google.maps.event.addListener(tanda, 'dragend', function(event){
			document.getElementById('latitude').value = this.getPosition().lat();
			document.getElementById('longitude').value = this.getPosition().lng();
	});
}
</script> 
<?php

$o = "";
	
if (isset($_GET['success']) && ($_GET['success'] == "1")) {

	$o .= '<div class="alert alert-success">
			Proses tambah kegiatan berhasil
			</div>';
			
} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
	
	$o .= '<div class="alert alert-error">
			Proses tambah kegiatan gagal
		   </div>';
}
		
echo $o;

?>
<div class="row">
	<div class="span8">
		<div class="control-group">
			<div id="map_canvas" style="width:100%; height:500px"></div>
		</div>
		<a href="?page=kegiatan"><i class="  icon-arrow-left"></i> kembali</a>
	</div>
	
	<form action="?page=addkegiatan&action=add" method="POST"> 
	<div class="span4">
	<div class="control-group">
	  <label class="control-label" for="input01">Nama Kegiatan</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="kegiatan" name="kegiatan" rel="popover" data-content="Masukkan nama kegiatan." data-original-title="Kegiatan">
	  </div>
	</div>
	
	<div class="control-group">
	  <label class="control-label" for="input02">Sektoral</label>
	  <div class="controls">
		<select name="sektoral" id="sektoral" onchange="set_icon(this.options[this.selectedIndex].value)" data-placeholder="Pilih sektoral..." class="chzn-select" style="width:280px;" tabindex="2">
		<?php
		
		/* memanggil fungsi untuk menampilkan data sektoral */
		require('../class/admin/sektoral.php');
		$db2 = new Sektoral();
		$table = "sektoral";
		$sektoral = $db2->lihat("sektoral", "idsektoral","");

		/* menampilkan data sektoral */
		$html .= '<option value=""></option>';
		foreach ($sektoral as $y) {
			$html .= '<option value="'.$y['idsektoral'].",".$y['icon'].'">'.$y['nama_sektoral'].'</option>';
		}

		echo $html;
	
		?>
		</select>
		</div>
	</div>
	
	
	<div class="control-group">
	  <label class="control-label" for="input01">Kabupaten</label>
	  <div class="controls">
		<select name="kabupaten" id="kabupaten" onchange="setpeta(this.options[this.selectedIndex].value)" data-placeholder="Pilih kabupaten..." class="chzn-select" style="width:280px;" tabindex="2">
		<?php
		
		/* memanggil fungsi untuk menampilkan data kabupaten */
		require('../class/admin/kabupaten.php');
		$db = new Kabupaten();
		$table = "kabkota";
		$kabupaten = $db->lihat($table, "idkabkota");
		/* menampilkan data kabupaten */
		if ($kabupaten) {
			$o .= '<option value=""></option>';
			foreach ($kabupaten as $l) {
				$o .= '<option value="'.$l['lat'].",".$l['long'].",".$l['idkabkota'].'">'.$l['nama_kabkota'].'</option>';
				/* echo "<a href=\"javascript:setpeta(".$l['lat'].",".$l['long'].",".$l['idkabkota'].")\">".$l['nama_kabkota']."</a><br>\n";
    			 echo "<span id=\"".$l['idkabkota']."\" style=\"display:none\"></span>\n";*/
			}
		} else {
			$o .= '<option value="">tidak ada data kabupaten</option>';
		}
		
		echo $o;
	
		?>
		</select>
		</div>
	</div>
	
	

	<div class="control-group">
		<label class="control-label" for="input01">Pagu</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="pagu" name="pagu" rel="popover" data-content="Masukkan pagu" data-original-title="Pagu">
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Polygon</label>
		  <div class="controls">
			<textarea class="input-xlarge" id="dataPanel" name="polygon"></textarea>
			<button id="showData" type="button" class="btn btn-primary">Tampilkan Koordinat</button>
			<button id="reset" value="Reset" type="button" class="btn btn-warning">Reset Polygon</button>
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Longitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="longitude" name="longitude" >
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01">Latitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="latitude" name="latitude">
		  </div>
	</div>
	
	<div class="control-group">
		<label class="control-label" for="input01"></label>
		  <div class="controls">
		   <button type="submit" class="btn btn-success" rel="tooltip" title="first tooltip">Tambah Kegiatan</button>
	
		  </div>
	</div>
	</form>

</div>
</div>
<?php


if ($_GET['action'] == "add") {

	$array 		= array();
	$kabupaten 	= $_POST['kabupaten'];
	$array    	= $kabupaten;
	$newarray 	= explode(",", $array);
	
	$array2 	= array();
	$sektoral	= $_POST['sektoral'];
	$array2    	= $sektoral;
	$newarray2 	= explode(",", $array2);
	
	
	$nama_kegiatan		= htmlentities(mysql_real_escape_string($_POST['kegiatan']));
	$kabkota			= htmlentities(mysql_real_escape_string($newarray[2]));
	$sektor				= htmlentities(mysql_real_escape_string($newarray2[0]));
	$pagu				= htmlentities(mysql_real_escape_string($_POST['pagu']));
	$polygon       		= $_POST['polygon'];
	$longitude			= htmlentities(mysql_real_escape_string($_POST['longitude']));
	$latitude			= htmlentities(mysql_real_escape_string($_POST['latitude']));
	$tahun				= date('Y');
	$username			= $_SESSION['username'];
	
	$db = new Kegiatan();
	$kegiatan = array ('nama_kegiatan'=>$nama_kegiatan, 
								 'idkabkota'=>$kabkota, 
								 'pagu'=>$pagu, 
								 'lat'=>$latitude, 
								 'long'=>$longitude,
								 'polygon'=>$polygon,
								 'tahun'=>$tahun,
								 'idsektoral'=>$sektor,
								 'username'=>$username);
	
	$addkegiatan = $db->tambah("kegiatan", $kegiatan);
	
	if ($addkegiatan) {
		?>
		<script language="javascript">
		document.location="?page=addkegiatan&success=1";
		</script>
		<?php
	} else {
		?>
			<script language="javascript">
			document.location="?page=addkegiatan&success=0";
			</script>
		<?php
	}
	
}
